package com.whut.whw.personalinfo.mythings;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;

import butterknife.BindView;

/**
 * <pre>
 *  desc: 我的物品，主要展示已发布和已完结的信息
 *  Created by 忘尘无憾 on 2017/06/23.
 *  version:
 * </pre>
 */

public class MyThingsActivity extends BaseActivity {

    @BindView(R.id.mythings_tab_top)
    TabLayout mythings_tab_top;//tab选项卡

    @BindView(R.id.mythings_viewpager)
    ViewPager mythings_viewpager;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private FragmentPagerAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_things;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initViewPager();
        initTabLayout();
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        final Fragment[] fragments = new Fragment[]{
                PushFragment.newInstance(),
                FinishFragment.newInstance()
        };
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        };
        mythings_viewpager.setAdapter(mAdapter);
    }

    /**
     * 初始化tab选项卡，需要在初始化viewpager之后
     */
    private void initTabLayout() {
        mythings_tab_top.setupWithViewPager(mythings_viewpager);//绑定viewpager，使之可联动
        mythings_tab_top.getTabAt(0).setText(getResources().getString(R.string.mythings_tab1));
        mythings_tab_top.getTabAt(1).setText(getResources().getString(R.string.mythings_tab2));
    }

    @Override
    public void setListener() {

        //toolbar的返回导航事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
